<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet">
    <style type="text/css">
        [v-cloak] {
          display: none;
        }
        fieldset {
            
        }
        table {
          width: 620px;
          border: 1px solid red;
          text-align: center;
        }
        thead {
          background-color: cornflowerblue;
        }
        #pop {
          position: absolute;
          left: 40%;
          top: 200px;
          z-index: 1999;
          background: rgba(0, 0, 0, 0.3);
          width: 380px;
          height: 100px;
          padding: 20px;
        }
    </style>
</head>
<body>
    <div id="app" class="content" v-cloak>
        <h3>学生录入系统</h3>
        <div>
            <label for="username">用户名:</label>
            <input type="text" id="username" class="form-control" placeholder="请输入用户名" v-model="newStudent.name">
        </div>
        <div>
            <label for="">密码:</label>
            <input type="text" name="" id="" class="form-control" value="" placeholder="请输入年纪" v-model="newStudent.age">
        </div>
        <div>
            <span>性别:</span>
            <select name="" id="" v-model="new-Student.sex" class=sel>
                <option value="男">男</option>
                <option value="女">女</option>
            </select>
        </div>
        <button @click="creatNewstydent()" class="btn btn-danger">创建新用户</button>
        <hr/>
        <h3 class="h3 text-info">用户信息表</h3>
        <button @click="agv()">显示所有学生的平均年纪</button>
        <button @click="sortedStudents()">按年龄升序显示学生信息</button>
        <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th>姓名</th>
                    <th>年龄</th>
                    <th>性别</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(student,index) in students">
                    <td>{{student.name}}</td>
                    <td>{{student.age}}</td>
                    <td>{{student.sex}}</td>
                    <td>
                        <button @click="deleteStudent(index)" class="btn btn-primary">删除</button>
                    </td>
                </tr>
            </tbody>
        </table>
        按姓名搜索:<input type="text" v-model="search"/>
        <button @click="filteredStudents()" class="btn btn-primary">搜索</button>
    </div>
    <script src="../js/vue.js"></script>
    <script type="text/javascript">
        var vm = new Vue({
            el: "#app",
            data: {
                students: [
                    { name: "张三", age: 20, sex: "男" },
                    { name: "李四", age: 22, sex: "男" },
                    { name: "王五", age: 21, sex: "女" },
                    { name: "赵四", age: 20, sex: "女" },
                ],
                newStudent:{name:"",age:0,sex:男},
                search:""
},
            methods: {
                creatNewstudent(){
                    //alert(0);
                    //姓名不能为空
                    if(this.newStudent.name===""){
                        alert("姓名不能为空");
                        return;
                    }
                    if(this.newStudent.age<10){
                        alert("年龄不能小于10");
                        return;
                    }
                    this.students.unshift(this.newStudent);
                    this.newStudent={name:"",age:0,sex:男};
                },
                deleteStudent(index){
                    this.students.splice(index,1);
                },
                filteredStudents(){
                    search=this.search;
                    vm.studnets=this.students.filter(function(item){
                        return item.name.match(search);
                    });
                },
                sortedStudents(){
                    this.students.sort(function(a,b){
                        return a.age>b.age;
                    })
                },
            },
            computed:{
                sum(){
                    var sum=0;
                    for(var i=0;i<this.students.length;i++){
                        sum+=this.students[i].age;
                    }
                    return sum;
                },
                avg(){
                   alert('平均年龄为:'+(this.sum/this.students.length));
                },
            }
        })
    </script>
</body>
</html>